<?
##########################################################################
#		DOCUMENTATION
##########################################################################
# Class that extends the rootclass. This module will provide the methods
# needed to render the index. 
#
# © LiberLab group, 2011
##########################################################################

require_once("base/root.php");

class Renderer extends Root{
	public function __construct() {
		parent::__construct("index");
	}
	
	public function main(){
		require_once("dataStructure/book.php");
		require_once("dataStructure/category.php");
		$numBooks = 0;
		$messages = $this->messages;

		$categoryClass = new Category();
		# top viewed in the home page
		$categories = $categoryClass->getCategories();
		$categoriesNum = count($categories);

		$indMaxCat = min(4, $categoriesNum);	
			
		$this->tagHash["catNameINDEX§§§§firstImgLinkINDEX§§§§firstBookCoverINDEX§§§§" . 
				"firstBookDescINDEX§§§§catImgLinkINDEXINDEX§§§§catBookCoverINDEXINDEX§§§§catBookDescINDEXINDEX"] = $indMaxCat;
		$cont = 0;
		# the categories are already ordered by booksNumber
		foreach($categories as $id => $category){	
			$categoryName = $category->category;
			$categoryBooks = $category->books;
			# the first book is rendered in a different way (when the javascript is disabled only the first books are shown)
			$firstBook = $categoryBooks[0];
			$firstBookId = $firstBook->bookId;
			$this->tagHash["firstBookCover$cont"] = $this->getCover($firstBookId, $firstBook->coverExtension);
			$title = ucfirst($firstBook->bookTitle);
			$temp = str_replace("__title__", $title, $messages["altText"]);
			$this->tagHash["firstBookDesc$cont"] = $temp;
			$this->tagHash["firstImgLink$cont"] = $this->defaultScriptName . "?page=book&amp;id=$firstBookId";

			$catIndMax = min(5, count($categoryBooks));
			$this->tagHash["catImgLink{$cont}INDEX§§§§catBookCover{$cont}INDEX§§§§catBookDesc{$cont}INDEX"] = $catIndMax;	
			for($catIndex=1; $catIndex<$catIndMax; $catIndex++){
				$singleBook = $categoryBooks[$catIndex];
				$singleBookId = $singleBook->bookId;
				$this->tagHash["catBookCover{$cont}{$catIndex}"] = $this->getCover($singleBookId ,$singleBook->coverExtension);
				$title = ucfirst($singleBook->bookTitle);
				$temp = str_replace("__title__", $title, $messages["altText"]);
				$this->tagHash["catBookDesc{$cont}{$catIndex}"] = $temp;	
				$this->tagHash["catImgLink{$cont}{$catIndex}"] = $this->defaultScriptName . "?page=book&amp;id=$singleBookId";	
			}
			$this->tagHash["catName" . $cont] = ucfirst($categoryName);
			$cont++;
		}	
		$bookClass = new Book();
		$views = $bookClass->getBasicData(4);
		$indMax = count($views);
		$this->tagHash["topImgLinkINDEX§§§§topViewedCoverINDEX§§§§topViewedAltINDEX"] = $indMax;
		
		for($index=0; $index<$indMax; $index++){
			$object = $views[$index];
			$this->tagHash["topImgLink$index"] = $this->defaultScriptName . "?page=book&amp;id={$object->id}";	
			$this->tagHash["topViewedCover$index"] = $this->getCover($object->id, $object->coverExtension);
			$temp = str_replace("__title__", $object->title, $messages["altText"]);
			$this->tagHash["topViewedAlt$index"] = $temp;
		}

		$lastViewed = $bookClass->getBasicData(4, "pubblicationData");
		$indMax = count($lastViewed);
		$this->tagHash["recImgLinkINDEX§§§§recentlyReleasedCoverINDEX§§§§recentlyReleasedAltINDEX"] = $indMax;	

		for($index = 0; $index < $indMax; $index++){
			$book = $lastViewed[$index];
			$title = $book->title;
			$this->tagHash["recentlyReleasedCover$index"] = $this->getCover($book->id, $book->coverExtension);
			$temp = str_replace("__title__", $title, $messages["altText"]);
			$this->tagHash["recentlyReleasedAlt$index"] = $temp;
			$this->tagHash["recImgLink$index"] = $this->defaultScriptName . "?page=book&amp;id={$book->id}";	
		}
		return $this->render();
	}

	public function __destruct() {
		parent::__destruct();
	}
};

?>
